package com.itany.boot.controller;

import com.itany.boot.entity.User;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 石小俊
 */
@Controller
@RequestMapping
public class HelloController {

    @RequestMapping("/sayHello")
    public ModelAndView sayHello(HttpSession session) {
        ModelAndView mav = new ModelAndView();
        mav.addObject("msg", "<span style='color:green;'>Hello Thymeleaf</span>");

        User user = new User(1, "admin", "111111");
        User user2 = new User(2, "jack", "222222");
        User user3 = new User(3, "alice", "333333");
        User user4 = new User(4, "tom", "444444");
        User user5 = new User(5, "lily", "555555");
        List<User> users = new ArrayList<>();
        users.add(user);
        users.add(user2);
        users.add(user3);
        users.add(user4);
        users.add(user5);

        session.setAttribute("user", user);
        session.getServletContext().setAttribute("user2", user2);

        mav.addObject("id", 10);
        mav.addObject("age", 18);
        mav.addObject("role","student");
        mav.addObject("users",users);
        mav.addObject("user",user);


        mav.setViewName("hello");
        return mav;
    }

}
